/// <reference name="MicrosoftAjax.debug.js" />
/// <reference path="../../Resources/js/jquery/jquery.debug.js" />


Type.registerNamespace("BM");

// CheckBoxListValueHandler object

BM.CheckBoxListValueHandler = function(leafItem) {
    BM.CheckBoxListValueHandler.initializeBase(this, [leafItem]);
}

BM.CheckBoxListValueHandler.prototype = {    
    
    getInputsPath: function() {
        return " input[type = checkbox]";        
    },               
    
    getValueSeparator: function() {
        return this.getInputsContainer().getAttribute('valueSeparator');
    },
    
    getValuesArray: function() {
        var array = new Array();     
        var inputs = this.getInputs();   
        for(var i = 0; i < inputs.length; i++) {
            if (inputs[i].checked) {                
                var inputValue = this.getInputItemValue(inputs[i]);
                if (inputValue) array[array.length] = inputValue;
            }
        }
        return array;
    },
    
    getControlValue: function() {
        return this.getValuesArray().join(this.getValueSeparator());        
    },
    
    getInputItemValue: function(input) {
        /// <returns type="string" >
        /// normally, this should be 'input.value'
        /// unfortunately Microsoft's CheckBox control does not allow setting 'value' attribute on the input[type=checkbox] (this is against W3C HTML 4.01 Specification - INPUT Element)
        /// to overcome this problem, the parent node of the checkboxes is given a custom attribute 'itemValue'
        /// </returns>
        return input.parentNode.getAttribute('itemValue');
    },

    setControlValue: function(value) {
        var values = value.split(this.getValueSeparator());        
        var inputs = this.getInputs();
        for(var i = 0; i < inputs.length; i++) {
            if (Array.contains(values, this.getInputItemValue(inputs[i]))) {
                inputs[i].checked = true;
            } else {
                inputs[i].checked = false;
            }
        }
    }       
}

BM.ValueHandler['CheckBoxListValueHandler'] = BM.CheckBoxListValueHandler;
BM.CheckBoxListValueHandler.registerClass("BM.CheckBoxListValueHandler", BM.InputsContainerValueHandler);